package exhi.action;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.stp.core.spring3.BaseSpringAction;
import com.stp.rbac.entity.Dept;
import com.stp.rbac.entity.UserInfo;
import com.stp.rbac.manager.DeptMng;
import com.stp.std.entity.ParamInfo;
import com.stp.std.manager.AuditLogMng;
import com.stp.std.manager.InitializableMng;
import com.stp.std.manager.ParamMng;
import com.stp.survey.entity.SurveyInfo;
import com.stp.survey.manager.SurveyMng;

import exhi.entity.EntpRelation;
import exhi.entity.EntpUser;
import exhi.entity.ExhiCfg;
import exhi.manager.EntpMng;
import exhi.manager.EntpRelationMng;

public class BaseAct extends BaseSpringAction {

	public boolean saveUpload(MultipartHttpServletRequest multipartRequest,
			String attribute) throws IllegalStateException, IOException {
		return saveUpload(multipartRequest, attribute, "upd",false);
	}

	protected String getSurveyId(String deptType) {
		String sql = "from SurveyInfo where surveyNotes='" + deptType
				+ "' and status='T'";
		List<SurveyInfo> lst = surveyMng.list(sql);
		String sid = "";
		for (SurveyInfo s : lst)
			sid = s.getId();
		return sid;
	}

	public void log(String type,String content){
		String userId=null;
		UserInfo user=getUser("userRbacEntp");
		if (userId!=null)
			log(user.getUserId(),type,content,null,null,null);
	}
	
	public void log(String type,String content,String p0,String p1){
		String userId=null;
		UserInfo user=getUser("userRbacEntp");
		if (userId!=null)
			log(user.getUserId(),type,content,p0,p1,null);
	}
	
	public EntpUser getEntpUser(){
		UserInfo user=getUser("userRbacEntp");
		return entpMng.get(user.getUserId());
	}

	public ExhiCfg getCfg() {
		ExhiCfg cfg = stdInitMng.getInitObj("exhiCfg", ExhiCfg.class);
		return cfg;
	}
	
	
	public String getStatus(String code){
		String s="";
		if (null==code || "".equals(code))
			return s;
		
		if ("01".equalsIgnoreCase(code))
			return "场馆预约";
		
		if ("05".equalsIgnoreCase(code))
			return "场馆确认+";
		
		if ("10".equalsIgnoreCase(code))
			return "项目预约";
		
		if ("20".equalsIgnoreCase(code))
			return "展览会申报";
		
		if ("30".equalsIgnoreCase(code))
			return "合同确认";
		
		if ("50".equalsIgnoreCase(code))
			return "打印报批件";
		
		if ("60".equalsIgnoreCase(code))
			return "展览会评估";
		
		if ("90".equalsIgnoreCase(code))
			return "展览会完成";
		
		return s;
	}
	
	public String getHref(String code,String Id){
		String s="";
		if (null==code || "".equals(code))
			return s;
		
		if ("01".equalsIgnoreCase(code))
			return "场馆预约";
		
		if ("05".equalsIgnoreCase(code))
			return "场馆确认+";
		
		if ("10".equalsIgnoreCase(code))
			return "项目预约";
		
		if ("20".equalsIgnoreCase(code))
			return "展览会申报";
		
		if ("30".equalsIgnoreCase(code))
			return "合同确认";
		
		if ("50".equalsIgnoreCase(code))
			return "打印报批件";
		
		if ("60".equalsIgnoreCase(code))
			return "展览会评估";
		
		if ("90".equalsIgnoreCase(code))
			return "展览会完成";
		
		return s;
	}
	
	public String getDeptName(String code){
		return deptMng.getDeptName(code);
	}
	
	public String getDeptTypeName(String code){
		String s="";
		List<ParamInfo> lst = paramMng.findByProperty("pCode",code);		
		for (ParamInfo d : lst) {
			s=d.getpName();
		}
		return s;
	}	

	public String getEntpRelaction(String projectId, String entpType) {
		String sql = " from EntpRelation where 1=1 ";
		sql += sqlModel.getSQLChars("projectId", "=", projectId, false, "and");
		sql += sqlModel.getSQLChars("type", "=", entpType, false, "and");
		List<EntpRelation> lst = relationMng.list(sql);
		String s = "";
		Dept dept;
		for (EntpRelation relation : lst) {
			if ("zldd".equalsIgnoreCase(entpType)){
				s += "," + relation.getEntpName();
				dept=deptMng.get(relation.getEntpId());
				if (null!=dept) {
					s += "("+getDeptTypeName(dept.getQyCode())+")";	
				}				
			}else{
				s += "," + relation.getEntpName();
			}
		
		}
		if ("".equalsIgnoreCase(s))
			s = ",";
		return s.substring(1);
	}

	@Autowired
	@Qualifier("entpRelationMng")
	protected EntpRelationMng relationMng;

	@Autowired
	@Qualifier("stdInitMng")
	protected InitializableMng stdInitMng;

	@Autowired
	@Qualifier("paramMng")
	protected ParamMng paramMng;

	@Autowired
	@Qualifier("deptMng")
	protected DeptMng deptMng;
	
	@Autowired
	@Qualifier("entpMng")
	protected EntpMng entpMng;
	
	@Autowired
	@Qualifier("auditLogMng")
	protected AuditLogMng auditLogMng;
	
	@Autowired
	@Qualifier("surveyMng")
	private SurveyMng surveyMng;

}
